import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { User } from './user';


@Injectable()
export class LoginDataProvider {
  
  private readonly baseUrl: string = 'https://api.bmob.cn/1';
  private readonly httpHeaders : HttpHeaders = new HttpHeaders({
    'X-Bmob-Application-Id':'7005414afe8e71b660439aac975c101f',
    'X-Bmob-REST-API-Key':'571bf16525c62447573dba6977e7e38e',
    'Content-Type':'application/json'
  });

  constructor(public http: HttpClient) { 
   
  }

  /**
   * d登录数据处理
   * @param username 
   * @param password 
   */
  login(username:string,password:string):Observable<User>{
    let _params = new HttpParams()
      .set('username',username)
      .set('password',password);
      return this.http.get<User>(`${this.baseUrl}/login`,{headers:this.httpHeaders,params:_params});
  }

  /**
   * 注册数据处理
   * @param username 
   * @param password 
   */
  register(username:string,password:string):Observable<User>{
    let registerUser = {username: username,password:password};
    return this.http.post<User>(`${this.baseUrl}/users`,registerUser,{headers:this.httpHeaders});
  }

}